﻿package core.ui.util
{
    import __AS3__.vec.*;
    import core.event.*;
    import core.image.*;
    import core.image.decoder.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.net.*;
    import flash.utils.*;

    public class BrushBrowser extends Dialog
    {
        var cache:Dictionary;
        var brushes:Vector.<Brush>;
        var pg:Progressbar;
        var credit:Text;
        var grouplist:Dropdown;
        var packlist:Selectbox;
        var holder:Sprite;
        var scroller:Scrollbar;

        public function BrushBrowser(param1:Tuyitu)
        {
            super(param1, "rm_brush_browser", 550, 295, true);
            this.cache = new Dictionary();
            this.packlist = new Selectbox(170, 200);
            this.packlist.x = 10;
            this.packlist.y = 52;
            this.packlist.addEventListener(SelectEvent.COMMAND, this.PackChange, false, 1, true);
            addChild(this.packlist);
            this.grouplist = new Dropdown(170, 170);
            this.grouplist.x = 10;
            this.grouplist.y = 25;
            this.grouplist.addEventListener(SelectEvent.COMMAND, this.GroupChange, false, 1, true);
            this.grouplist.AddItem("艺术效果", "artistic", true);
            this.grouplist.AddItem("自然界", "nature");
            this.grouplist.AddItem("化装", "makeup");
            this.grouplist.AddItem("形状", "shapes");
            this.grouplist.AddItem("其它", "misc");
            this.grouplist.Bind();
            addChild(this.grouplist);
            this.holder = new Sprite();
            this.holder.x = 180;
            this.holder.y = 25;
            addChild(this.holder);
            this.scroller = new Scrollbar(this.holder, new Rectangle(0, 0, 350, 225));
            this.scroller.x = 530;
            this.scroller.y = 25;
            addChild(this.scroller);
            this.pg = new Progressbar(100);
            this.pg.x = 310;
            this.pg.y = 100;
            this.pg.visible = false;
            addChild(this.pg);
            this.credit = new Text("", 11, 0, false);
            this.credit.htmlText = "";
            this.credit.x = 10;
            this.credit.y = 268;
            addChild(this.credit);
            this.GroupChange(new SelectEvent(SelectEvent.COMMAND, "artistic"));
            ok.visible = false;
            ok.title.text = U.RM("rm_add");
            cancel.title.text = U.RM("rm_close");
            var _loc_2:int = 265;
            cancel.y = 265;
            ok.y = _loc_2;
            ok.x = 389;
            cancel.x = 469;
            return;
        }// end function

        private function GroupChange(event:SelectEvent) : void
        {
            this.Clear();
            this.packlist.Clear();
            var _loc_2:* = new URLLoader();
            _loc_2.dataFormat = URLLoaderDataFormat.TEXT;
            _loc_2.addEventListener(Event.COMPLETE, this.ParseGroupList);
            _loc_2.addEventListener(IOErrorEvent.IO_ERROR, this.ErrorMessage);
            _loc_2.load(new URLRequest("/_brush/rs/" + event.val + ".rs"));
            return;
        }// end function

        private function LoaderProgress(event:ProgressEvent) : void
        {
            this.pg.Set(event.bytesLoaded <= 0 ? (0) : (event.bytesLoaded / event.bytesTotal));
            return;
        }// end function

        private function PackChange(event:SelectEvent) : void
        {
            var loader:*;
            var _arg1:* = event;
            loader;
            var e:* = _arg1;
            this.Clear();
            ok.visible = false;
            this.pg.visible = true;
            this.pg.Set(0);
            loader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.BINARY;
            loader.addEventListener(Event.COMPLETE, function ()
            {
                DecodePBR(loader.data);
                return;
            }// end function
            );
            loader.addEventListener(IOErrorEvent.IO_ERROR, this.ErrorMessage);
            loader.addEventListener(ProgressEvent.PROGRESS, this.LoaderProgress);
            if (e.val.indexOf("|") == -1)
            {
                loader.load(new URLRequest("/_brush/pbr/" + e.val + ".pbr"));
                this.credit.htmlText = "";
            }
            else
            {
                loader.load(new URLRequest("/_brush/pbr/" + e.val.split("|")[0].replace(/^\s+|\s+$""^\s+|\s+$/g, "") + ".pbr"));
                this.credit.htmlText = e.val.split("|")[1].replace(/^\s+|\s+$""^\s+|\s+$/g, "");
            }
            return;
        }// end function

        private function ParseGroupList(event:Event) : void
        {
            var _loc_2:Array = null;
            var _loc_4:int = 0;
            var _loc_3:* = (event.target.data as String).split("\n");
            while (_loc_4 < _loc_3.length)
            {
                
                _loc_2 = _loc_3[_loc_4].split("§");
                if (_loc_2.length > 1)
                {
                    this.packlist.AddItem((_loc_2[1] as String).replace(/^\s+|\s+$""^\s+|\s+$/g, ""), (_loc_2[0] as String).replace(/^\s+|\s+$""^\s+|\s+$/g, ""), _loc_2.length > 2 ? (_loc_2[2]) : (""));
                }
                _loc_4++;
            }
            return;
        }// end function

        private function DecodePBR(ok:ByteArray) : void
        {
            var _loc_2:* = new PBRDecoder();
            this.brushes = _loc_2.decode(ok);
            this.RenderBrushes();
            this.pg.visible = false;
            return;
        }// end function

        public function RenderBrushes() : void
        {
            var _loc_1:BrushInfo = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            this.Clear();
            while (_loc_4 < this.brushes.length)
            {
                
                if (_loc_4 > 0 && _loc_4 % 10 == 0)
                {
                    _loc_3 = _loc_3 + 45;
                    _loc_2 = 0;
                }
                _loc_1 = new BrushInfo(this.brushes[_loc_4] as Brush);
                _loc_1.x = _loc_2;
                _loc_1.y = _loc_3;
                _loc_1.nr = _loc_4;
                this.holder.addChild(_loc_1);
                _loc_2 = _loc_2 + 35;
                _loc_4++;
            }
            this.scroller.SetContentHeight(_loc_3 + 45);
            ok.visible = true;
            return;
        }// end function

        public function Clear() : void
        {
            while (this.holder.numChildren > 0)
            {
                
                this.holder.removeChildAt(0);
            }
            this.scroller.SetContentHeight(0);
            return;
        }// end function

        public function ErrorMessage(... args) : void
        {
            this.pg.visible = false;
            new AlertDialog(ed, "rm_error_occurred");
            return;
        }// end function

        override public function OKClick(event:MouseEvent) : void
        {
            ed.options.brushes.AddPack(this.brushes);
            Close();
            return;
        }// end function

    }
}
